package com.sunshine.wrongquestionset.activity.main

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.textview.MaterialTextView
import com.sunshine.wrongquestionset.R
import com.sunshine.wrongquestionset.activity.question_set.QuestionSetActivity
import com.sunshine.wrongquestionset.callback.OnClickListener
import com.sunshine.wrongquestionset.room.entity.SubjectEntity

/**
 * @author sunshine
 * @date 2021/4/10
 * @param callback 处理整体点击添加、长按修改的回调
 */
class SubjectRecyclerAdapter(
    private val list: ArrayList<SubjectEntity>?,
    private val owner: LifecycleOwner,
    private val viewModel: MainViewModel,
    private val callback: OnClickListener
) : RecyclerView.Adapter<SubjectRecyclerAdapter.ViewHolder>() {

    private var backgroundList: ArrayList<Int> = ArrayList()
    private lateinit var context: Context
    private var maxPager: Int = list?.size?.plus(1)?:1

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val root: CardView = itemView.findViewById(R.id.root_subject)
        //颜色条View
        val viewColor: View = itemView.findViewById(R.id.view_color)
        val subjectTextView: MaterialTextView = itemView.findViewById(R.id.textView_subject)
        val info: MaterialTextView = itemView.findViewById(R.id.textView_info)
        val addSubject: ImageView = itemView.findViewById(R.id.imageView_add_subject)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        context = parent.context
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_subject, parent, false))
    }

    override fun getItemCount(): Int {
        //默认有一个添加
        return maxPager
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        when(position) {
            maxPager - 1 -> {
                holder.addSubject.visibility = View.VISIBLE
                holder.root.setOnClickListener {
                    //点击添加科目
                    callback.onClick(position)
                }
            }
            else -> {
                holder.subjectTextView.text = list!![position].name

                viewModel.getQuestionSetCountBySubjectId(list[position].id).observe(owner, Observer {
                    holder.info.text = "${it}个错题本"
                })
                holder.viewColor.background = ContextCompat.getDrawable(context, backgroundList[position % backgroundList.size])
                //holder.root.background = ContextCompat.getDrawable(context, backgroundList[position % backgroundList.size])
                holder.root.setOnClickListener {
                    val intent = Intent(context, QuestionSetActivity::class.java)
                    intent.putExtra("subjectId", list[position].id)
                    intent.putExtra("subjectName", list[position].name)
                    context.startActivity(intent)
                }
                holder.root.setOnLongClickListener {
                    callback.onLongClick(position)
                    true
                }
            }
        }
    }

    init {
//        backgroundList.apply {
//            add(R.drawable.ripple_item_bg_1)
//            add(R.drawable.ripple_item_bg_2)
//            add(R.drawable.ripple_item_bg_3)
//            add(R.drawable.ripple_item_bg_4)
//            add(R.drawable.ripple_item_bg_5)
//            add(R.drawable.ripple_item_bg_6)
//        }
        backgroundList.apply {
            add(R.color.item_background_solid_1)
            add(R.color.item_background_solid_2)
            add(R.color.item_background_solid_3)
            add(R.color.item_background_solid_4)
            add(R.color.item_background_solid_5)
            add(R.color.item_background_solid_6)
        }
    }
}